//
//  CustomProjectContentView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/21.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "CustomProjectContentView.h"

//
#import <OCSkeleton.h>

//上边距
static CGFloat CustomProjectContentViewTopPadding = 23.0;
//下边距
static CGFloat CustomProjectContentViewBottomPadding = 15.0;
//左以及右边距
static CGFloat CustomProjectContentViewLeftRightPadding = 13.0;


//除掉边距的内容高度
static CGFloat CustomProjectContentViewContentHeight = 146.0;

//本周期回报率最大宽度
static CGFloat CustomProjectContentViewRateCaptionMaxWidth = 80.0;


@interface CustomProjectContentView () <OCGradientsOwner>

/**
 *optimizationImage
 */
@property (nonatomic, strong) UIImage *optimizationImage;

/**
 *contentEdgeInsets 初始化时距离父视图的内边距
 */
@property (nonatomic, assign) UIEdgeInsets contentEdgeInsets;


#pragma mark - ContainerViews
/**
 *titleContainerView
 */
@property (nonatomic, strong) OCGradientContainerView *titleContainerView;
/**
 *rateCaptionContainerView
 */
@property (nonatomic, strong) OCGradientContainerView *rateCaptionContainerView;
/**
 *rateContainerView
 */
@property (nonatomic, strong) OCGradientContainerView *rateContainerView;
/**
 *displayCollectionContainerView
 */
@property (nonatomic, strong) OCGradientContainerView *displayCollectionContainerView;
@property (nonatomic, strong) OCGradientContainerView *totleTitleContainerView;
@property (nonatomic, strong) OCGradientContainerView *totleDataContainerView;
@property (nonatomic, strong) OCGradientContainerView *lastTitleContainerView;
@property (nonatomic, strong) OCGradientContainerView *lastDataContainerView;
//
@property (nonatomic, strong) OCGradientContainerView *countdownContainerView;

/**
 *gradientLayers
 */
@property (nonatomic, strong) NSMutableArray *oc_gradientLayers;
/**
 *skeletonViews
 */
@property (nonatomic, strong) NSMutableArray *oc_skeletonViews;

@end

@implementation CustomProjectContentView

- (UIImage *)optimizationImage {
    if (!_optimizationImage) {
        _optimizationImage = LOAD_IMAGE(@"home_opt_cell_rect");
    }
    return _optimizationImage;
}

- (instancetype)initWithContentEdgeInsets:(UIEdgeInsets)edgeInsets
{
    self = [super init];
    if (self) {
        self.contentEdgeInsets = edgeInsets;
        [self setupSubviews];
        [self setContainerViews];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.contentEdgeInsets = [self.class normalEdgeInsets];
        [self setupSubviews];
        [self setContainerViews];
    }
    return self;
}
- (void)setupSubviews {
    self.backgroundColor = [UIColor whiteColor];
    //初始化projectLabel
    self.projectLabel = [YYLabel new];
//    self.projectLabel.font = [UIFont systemFontOfSize:19 weight:UIFontWeightMedium];
//    self.projectLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.projectLabel.numberOfLines = 2;
    [self addSubview:self.projectLabel];
    [self.projectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentEdgeInsets.top);
        make.left.mas_equalTo(self.contentEdgeInsets.left);
        make.right.mas_equalTo(-self.contentEdgeInsets.right-CustomProjectContentViewRateCaptionMaxWidth);
//        make.height.mas_equalTo(CGFLOAT_MAX);
    }];
    //
    self.projectTagView = [BCProjectTagView new];
    //
    self.rateCaptionLabel = [UILabel new];
    self.rateCaptionLabel.textAlignment = NSTextAlignmentRight;
    self.rateCaptionLabel.font = [UIFont systemFontOfSize:11 weight:UIFontWeightRegular];
    self.rateCaptionLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.rateCaptionLabel.adjustsFontSizeToFitWidth = true;
    self.rateCaptionLabel.numberOfLines = 2;
    [self addSubview:self.rateCaptionLabel];
    [self.rateCaptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(self.contentEdgeInsets.top);
        make.right.mas_equalTo(self).offset(-self.contentEdgeInsets.right);
        make.width.mas_lessThanOrEqualTo(CustomProjectContentViewRateCaptionMaxWidth);
    }];
    //
    self.rateLabel = [UILabel new];
    self.rateLabel.textAlignment = NSTextAlignmentRight;
    self.rateLabel.font = [UIFont systemFontOfSize:23 weight:UIFontWeightMedium];
    self.rateLabel.textColor = [UIColor darkMainColor];
    [self addSubview:self.rateLabel];
    [self.rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.rateCaptionLabel);
        make.top.mas_equalTo(self.rateCaptionLabel).offset(self.rateCaptionLabel.font.lineHeight*2+2.0);
        make.height.mas_equalTo(self.rateLabel.font.lineHeight);
    }];
    //
    self.displayCollection = [[HomeCellDisplayCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[UICollectionViewFlowLayout new]];
    [self addSubview:self.displayCollection];
    [self.displayCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.projectLabel.mas_left);
         make.right.mas_equalTo(self).offset(-100.0);
        make.centerY.mas_equalTo(self.rateLabel);
        make.height.mas_equalTo([HomeCellDisplayCollectionView defaultHeight]);
    }];
    //
    self.totleDataView = [HomeCellDataView new];
    [self addSubview:self.totleDataView];
    [self.totleDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.rateLabel.mas_bottom).offset(16.0);
        make.left.mas_equalTo(self).offset(self.contentEdgeInsets.left);
        make.right.mas_equalTo(self.mas_centerX).offset(-self.contentEdgeInsets.left/2);
        make.height.mas_equalTo([HomeCellDataView defaultHeight]);
    }];
    self.lastDataView = [HomeCellDataView new];
    [self addSubview:self.lastDataView];
    [self.lastDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.totleDataView);
        make.left.mas_equalTo(self.mas_centerX).offset(self.contentEdgeInsets.left/2);
        make.right.mas_equalTo(self).offset(-self.contentEdgeInsets.right);
        make.height.mas_equalTo([HomeCellDataView defaultHeight]);
    }];
    //
    UIView *countdownPreferenceView = [UIView new];
    [self addSubview:countdownPreferenceView];
    [countdownPreferenceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lastDataView.mas_bottom);
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
    self.countdownView = [CustomProjectCountDownView new];
    [self addSubview:self.countdownView];
    [self.countdownView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(self.contentEdgeInsets.left);
        make.right.mas_equalTo(self).offset(-self.contentEdgeInsets.right);
        make.centerY.mas_equalTo(countdownPreferenceView);
        make.height.mas_equalTo([CustomProjectCountDownView defaultHeight]);
    }];
}
- (void)setContainerViews {
    self.titleContainerView = [OCGradientContainerView new];
    [self addSubview:self.titleContainerView];
    [self.titleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.projectLabel);
        make.top.mas_equalTo(self.projectLabel);
        make.height.mas_equalTo(self.projectLabel.font.lineHeight);
        make.right.mas_equalTo(self).offset(-60.0-self.contentEdgeInsets.right-10.0);
    }];
    [self.oc_skeletonViews addObject:self.titleContainerView];
    [self.oc_gradientLayers addObject:self.titleContainerView.gradientLayer];
    //
    self.rateCaptionContainerView = [OCGradientContainerView new];
    [self addSubview:self.rateCaptionContainerView];
    [self.rateCaptionContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.rateCaptionLabel);
        make.centerY.mas_equalTo(self.titleContainerView);
        make.height.mas_equalTo(self.rateCaptionLabel.font.lineHeight);
        make.left.mas_equalTo(self.titleContainerView.mas_right).offset(10.0);
    }];
    [self.oc_skeletonViews addObject:self.rateCaptionContainerView];
    [self.oc_gradientLayers addObject:self.rateCaptionContainerView.gradientLayer];
    //
    self.rateContainerView = [OCGradientContainerView new];
    [self addSubview:self.rateContainerView];
    [self.rateContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.rateLabel);
        make.right.mas_equalTo(self.rateLabel);
        make.height.mas_equalTo(self.rateLabel);
        make.width.mas_equalTo(65.0);
    }];
    [self.oc_skeletonViews addObject:self.rateContainerView];
    [self.oc_gradientLayers addObject:self.rateContainerView.gradientLayer];
    //
    self.displayCollectionContainerView = [OCGradientContainerView new];
    [self addSubview:self.displayCollectionContainerView];
    [self.displayCollectionContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.displayCollection);
        make.centerY.mas_equalTo(self.rateLabel);
        make.height.mas_equalTo(self.displayCollection);
        make.width.mas_equalTo(150.0);
    }];
    [self.oc_skeletonViews addObject:self.displayCollectionContainerView];
    [self.oc_gradientLayers addObject:self.displayCollectionContainerView.gradientLayer];
    //
    self.totleTitleContainerView = [OCGradientContainerView new];
    [self addSubview:self.totleTitleContainerView];
    [self.totleTitleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.totleDataView.titleLabel);
        make.centerY.mas_equalTo(self.totleDataView.titleLabel);
        make.height.mas_equalTo(self.totleDataView.titleLabel);
        make.width.mas_equalTo(100.0);
    }];
    [self.oc_skeletonViews addObject:self.totleTitleContainerView];
    [self.oc_gradientLayers addObject:self.totleTitleContainerView.gradientLayer];
    //
    self.totleDataContainerView = [OCGradientContainerView new];
    [self addSubview:self.totleDataContainerView];
    [self.totleDataContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.totleDataView.dataLabel);
        make.centerY.mas_equalTo(self.totleDataView.dataLabel);
        make.height.mas_equalTo(self.totleDataView.dataLabel);
        make.width.mas_equalTo(120.0);
    }];
    [self.oc_skeletonViews addObject:self.totleDataContainerView];
    [self.oc_gradientLayers addObject:self.totleDataContainerView.gradientLayer];
    //
    self.lastTitleContainerView = [OCGradientContainerView new];
    [self addSubview:self.lastTitleContainerView];
    [self.lastTitleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.lastDataView.titleLabel);
        make.centerY.mas_equalTo(self.lastDataView.titleLabel);
        make.height.mas_equalTo(self.lastDataView.titleLabel);
        make.width.mas_equalTo(100.0);
    }];
    [self.oc_skeletonViews addObject:self.lastTitleContainerView];
    [self.oc_gradientLayers addObject:self.lastTitleContainerView.gradientLayer];
    //
    self.lastDataContainerView = [OCGradientContainerView new];
    [self addSubview:self.lastDataContainerView];
    [self.lastDataContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.lastDataView.dataLabel);
        make.centerY.mas_equalTo(self.lastDataView.dataLabel);
        make.height.mas_equalTo(self.lastDataView.dataLabel);
        make.width.mas_equalTo(120.0);
    }];
    [self.oc_skeletonViews addObject:self.lastDataContainerView];
    [self.oc_gradientLayers addObject:self.lastDataContainerView.gradientLayer];
    //
    self.countdownContainerView = [OCGradientContainerView new];
    [self addSubview:self.countdownContainerView];
    [self.countdownContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.countdownView);
        make.right.mas_equalTo(self.countdownView);
        make.centerY.mas_equalTo(self.countdownView);
        make.height.mas_equalTo([CustomProjectCountDownView textFont].lineHeight);
    }];
    [self.oc_skeletonViews addObject:self.countdownContainerView];
    [self.oc_gradientLayers addObject:self.countdownContainerView.gradientLayer];
}
- (NSMutableArray *)oc_gradientLayers {
    if (!_oc_gradientLayers) {
        _oc_gradientLayers = [NSMutableArray array];
    }
    return _oc_gradientLayers;
}
- (NSMutableArray *)oc_skeletonViews {
    if (!_oc_skeletonViews) {
        _oc_skeletonViews = [NSMutableArray array];
    }
    return _oc_skeletonViews;
}
#pragma mark - OCGradientsOwner
- (NSArray <OCGradientLayer *>*)gradientLayers {
    return  self.oc_gradientLayers;
}
- (NSArray <UIView *>*)skeletonViews {
    return self.oc_skeletonViews;
}
#pragma mark - 设置加载placeholderView
- (void)clearContainerViews {
    for (OCGradientContainerView *view in self.oc_skeletonViews) {
        view.backgroundColor = [UIColor clearColor];
        view.alpha = 0;
    }
}
- (void)showContainerViews {
    for (OCGradientContainerView *view in self.oc_skeletonViews) {
        view.backgroundColor = [OCGradientContainerView appearance].backgroundColor;
        view.alpha = 1;
    }
}

#pragma mark - 设置数据
- (void)setContentData:(BCProjectModel *)contentData {
    _contentData = contentData;
    if (contentData) {
        [self clearContainerViews];
        //
        self.countdownView.alpha = 1;
        self.displayCollection.alpha = 1;
        
        [self.countdownView setCountdownString:contentData.countdownMess endTime:contentData.end_time];
        //设置数据
        [self setProjectLabelContent:contentData];
        //
        self.rateCaptionLabel.text = contentData.repayCaption;
        self.rateLabel.text = contentData.repay;
        
        //
        self.totleDataView.title = contentData.target_title;
        self.totleDataView.data = contentData.target;
        self.lastDataView.title = contentData.surplus_title;
        self.lastDataView.data = contentData.surplus;
        
        self.displayCollection.displayArray = [contentData.tags componentsSeparatedByString:@","];
        //
        self.countdownView.timeConsumingString = contentData.taking;
        
    }else {
        self.projectLabel.attributedText = nil;
        
        self.countdownView.alpha = 0;
        self.displayCollection.alpha = 0;
        self.rateCaptionLabel.text = nil;
        self.rateLabel.text = nil;
        
        //
        self.totleDataView.title = nil;
        self.totleDataView.data = nil;
        self.lastDataView.title = nil;
        self.lastDataView.data = nil;
        [self showContainerViews];
    }
    [self layoutSubviews];
}
/**
 设置project名称
 */
- (void)setProjectLabelContent:(BCProjectModel *)contentData {
    NSString *hot = contentData.hot;
    NSString *projectName = contentData.project_name;
    //
    NSMutableAttributedString *projectAttributedText = [[NSMutableAttributedString alloc] init];
    //
    UIFont *font = [UIFont systemFontOfSize:19 weight:UIFontWeightMedium];
    if (hot.length) {
        UIColor *tintColor;
        @try {
            tintColor = [UIColor qmui_colorWithHexString:contentData.hot_color];
        } @catch (NSException *exception) {
            
        } @finally {
            
        }
        if (!tintColor) {
            tintColor = [UIColor qmui_colorWithHexString:@"#3D5BFF"];
        }
        self.projectTagView.tintColor = tintColor;
        self.projectTagView.alertTag = hot;
        CGSize size = CGSizeMake(CGRectGetWidth(self.projectTagView.frame)+7.0, CGRectGetHeight(self.projectTagView.frame));
        NSAttributedString *hotAttributedText = [NSAttributedString yy_attachmentStringWithContent:self.projectTagView contentMode:UIViewContentModeLeft attachmentSize:size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
        [projectAttributedText appendAttributedString:hotAttributedText];
    }
    NSAttributedString *nameAttributedText = [[NSAttributedString alloc] initWithString:projectName attributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor qmui_colorWithHexString:@"#666666"]}];
    [projectAttributedText appendAttributedString:nameAttributedText];
    //
    self.projectLabel.attributedText = projectAttributedText;
}


+ (UIEdgeInsets)normalEdgeInsets {
    return UIEdgeInsetsMake(CustomProjectContentViewTopPadding, CustomProjectContentViewLeftRightPadding, CustomProjectContentViewBottomPadding, CustomProjectContentViewLeftRightPadding);
}

+ (CGFloat)defaultHeight {
    return [self defaultHeightWithEdgeInsets:[self normalEdgeInsets]];
}
+ (CGFloat)defaultHeightWithEdgeInsets:(UIEdgeInsets)edgeInsets {
    CGFloat height = 0;
    height += edgeInsets.top;
    height += CustomProjectContentViewContentHeight;
    height += edgeInsets.bottom;
    return height;
}

@end
